import React, { useState } from 'react'
import Taro, { useDidShow } from '@tarojs/taro'
import {View, Input} from '@tarojs/components'
import './index.less'

export default function CouponAdd() {
  const [code,setCode] = useState('')
  const [selectMerchant, setselectMerchant] = useState({})
  useDidShow(()=>{
    setselectMerchant(Taro.getStorageSync('biddingCardMerchant') || Taro.getStorageSync('selectMerchant'))
  })
  return (
    <View className='coupon-add'>
      <View className="add-header" onClick={()=>{
        Taro.navigateTo({
          url:`/mainPages/select-cinema/index?type=bidding`
        })
      }}>
        {selectMerchant.cinemaNickName}
        <View className="at-icon at-icon-chevron-right"></View>
      </View>
      <View className="add-list">
        <View className="list-item">
          <View className="item-label">输入券码</View>
          <Input className='input' value={code} onInput={e=>{
            setCode(e.target.value)
          }} placeholder='请输入优惠券兑换码' />
        </View>
      </View>
      <View className="add-p">添加的优惠券和兑换券，可在选择优惠券或兑换券时使用，也可在"我的-优惠券"查看。</View>
      <View className="add-footer">
        <View className="footer-button" onClick={async ()=>{
          if(code){
            const data = {
              couponNo:code,
              cid: selectMerchant.id,
            }
            Taro.showLoading()
            const res = await Taro.$post('/wallet/app/coupon/couponBind',data,'1')
            setCode('')
            if(res.code == '1'){
              Taro.$toast('提交成功','success')
              setTimeout(() => {
                setCode('')
              }, 1000);
            }else{
              Taro.$toast(res.message)
            }
          }
        }}>确认</View>
      </View>
    </View>
  )
}
